/**
 * @FilePath: shared_parameters\src\param_reader_node.cpp
 * @Description:  
 * @Author: haibo
 * @Version: 1.0.0
 * @LastEditors: haibo ashengzi@163.com
 * @LastEditTime: 2025-02-19 14:44:21
 * @Copyright (c) 2025 by 临工智能信息科技有限公司, All Rights Reserved. 
**/


#include <rclcpp/rclcpp.hpp>
#include <shared_parameters/shared_params_interface.hpp>
#include <chrono>
#include <random>
using namespace std::chrono_literals;

class ParamReaderNode : public rclcpp::Node {
public:
    ParamReaderNode() : Node("param_reader") {
        // 初始化共享内存接口
        if (!SharedParamsInterface::initialize()) {
            RCLCPP_ERROR(this->get_logger(), "Failed to initialize shared memory interface");
            return;
        }

        // 创建定时器，每秒读取一次参数
        timer_ = this->create_wall_timer(
            100ms, std::bind(&ParamReaderNode::timer_callback, this));

        RCLCPP_INFO(this->get_logger(), "Parameter reader node started");
    }

private:

    void randomizeString(std::string& s) {
        static std::random_device rd;
        static std::mt19937 gen(rd());
        static std::uniform_int_distribution<> dis('a', 'z');

        for (auto& c : s) {
            c = static_cast<char>(dis(gen));
        }
    }

    void timer_callback() {
        timer_->cancel();
        // try {
        //     auto params = SharedParamsInterface::getNodeParams("param_writer");
        //     std::cout << "Get param_writer params:" << std::endl;
        //     for (const auto& [name, value] : params) {
        //         // 使用参数名和值
        //         RCLCPP_INFO_STREAM(this->get_logger(), name << " = " << value.toString());
        //     }
        // } catch (const std::exception& e) {
        //     RCLCPP_ERROR(this->get_logger(), "Error reading parameters: %s", e.what());
        // }

        // try {
        //     auto params = SharedParamsInterface::getNodeParams("param_reader");
        //     std::cout << "Get param_reader params:" << std::endl;
        //     for (const auto& [name, value] : params) {
        //         // 使用参数名和值
        //         RCLCPP_INFO_STREAM(this->get_logger(), name << " = " << value.toString());
        //     }
        // } catch (const std::exception& e) {
        //     RCLCPP_ERROR(this->get_logger(), "Error reading parameters: %s", e.what());
        // }
       
        // try {
        //     static int count = 0;
        //     static double double_value = 0;
        //     static bool bool_value = false;
        //     static std::string str = "reader";
        //     RCLCPP_INFO_STREAM(this->get_logger(), 
        //         "write int:" << count << 
        //         ",double:"<< double_value << 
        //         ",bool:"<<bool_value <<
        //         ",str:" << str);
        //     SharedParamsInterface::ParamMap params;
        //     params["param1"] = SharedParamsInterface::ParamValue(count++);
        //     params["param2"] = SharedParamsInterface::ParamValue(double_value);
        //     params["param3"] = SharedParamsInterface::ParamValue(str);
        //     params["param4"] = SharedParamsInterface::ParamValue(bool_value);
        //     double_value += 0.01;
        //     bool_value = !bool_value;
        //     randomizeString(str);
        //     // std::cout << "write param_reader params" << std::endl;
        //     SharedParamsInterface::setNodeParams("param_reader", params);
        // } catch (const std::exception& e) {
        //     RCLCPP_ERROR(this->get_logger(), "Error updating parameters: %s", e.what());
        // }

        try {
            auto all_params = SharedParamsInterface::listAllParams();
            std::cout << "list all params:" << std::endl;
            for (const auto& info : all_params) {
                std::cout << "Node: " << info.node_name 
                          << ", Param: " << info.param_name
                          << ", Type: " << info.param_type
                          << ", Value: " << info.param_value << std::endl;
            }
        } catch (const std::exception& e) {
            RCLCPP_ERROR(this->get_logger(), "Error list parameters: %s", e.what());
        }

        rclcpp::shutdown();
    }

    rclcpp::TimerBase::SharedPtr timer_;
};

int main(int argc, char** argv) {
    rclcpp::init(argc, argv);
    rclcpp::spin(std::make_shared<ParamReaderNode>());
    rclcpp::shutdown();
    return 0;
}